package com.example.volleyball.Tools;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AccountTools {

    SQLiteDatabase db;

    public AccountTools(AccountDB db) {
        this.db = db.getWritableDatabase();
    }


    /**
     * 向Account表中插入账户和密码
     *
     * @param account  账户
     * @param password 密码
     */
    public void insert(String account, String password) {
        ContentValues values = new ContentValues();
        values.put("account", account);
        values.put("passsword", password);
        db.insert("Account", null, values);
    }


    /**
     * 根据账户和密码信息向Account表中查询数据, 是否有该用户信息
     *
     * @param account  账户
     * @param password 密码
     * @return 数据库查询到返回 true, 否则返回 false
     */
    public boolean select(String account, String password) {
        boolean b = false;
        Cursor cursor = db.query("Account", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                b = false;
                String s_account = cursor.getString(cursor.getColumnIndex("account"));
                String s_password = cursor.getString(cursor.getColumnIndex("passsword"));
                if (s_account.equals(account) && s_password.equals(password)) {
                    b = true;
                    break;
                }
            } while (cursor.moveToNext());
        }
        return b;
    }

    /**
     * 根据账户信息向Account表中查询数据, 是否有此用户名
     *
     * @param account 账户
     * @return 数据库查询到返回 true, 否则返回 false
     */
    public boolean select(String account) {
        boolean b = false;
        Cursor cursor = db.query("Account", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                b = false;
                String s_account = cursor.getString(cursor.getColumnIndex("account"));
                if (s_account.equals(account)) {
                    b = true;
                    break;
                }
            } while (cursor.moveToNext());
        }
        return b;
    }
}
